package com.emapp.lccode.fragment

import android.os.Bundle
import androidx.lifecycle.Observer
import com.emapp.lccode.R
import com.emapp.lccode.activity.ReportInforKtActivity
import com.emapp.lccode.adapter.MyOrderKtAdapter
import com.emapp.lccode.base.BaseConfig
import com.emapp.lccode.base.BaseFragment1
import com.emapp.lccode.base.EventBusConfig.REFRESH_ADDRESS
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.bean.DeviceKt
import com.emapp.lccode.bean.OrderKt
import com.emapp.lccode.databinding.FragmentListBinding
import com.emapp.lccode.mode.OrderKtViewModel
import com.emapp.lccode.utils.MmkvUtil
import com.emapp.lccode.utils.toEntity
import com.emapp.lccode.utils.toStartActivity
import com.emapp.lccode.utils.vertical
import com.hjq.toast.Toaster
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import me.hgj.jetpackmvvm.ext.parseState
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ReportListFragment : BaseFragment1<OrderKtViewModel, FragmentListBinding>() {

    var datas: ArrayList<OrderKt> = arrayListOf()
    var start_time = ""
    var end_time = ""
    var keywords = ""
    var status = ""
    var device_id = ""
    var page = 1
    private val orderKtAdapter: MyOrderKtAdapter by lazy { MyOrderKtAdapter() }

    companion object {
        fun newInstance(status: String): ReportListFragment {
            val args = Bundle()
            args.putString("status", status)
            val fragment = ReportListFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        EventBus.getDefault().register(this)
        //  status = arguments?.getString("status").toString()
        val inf = MmkvUtil.getString("device", "")
        if (inf.isNotEmpty()) {
            val device = inf.toEntity<DeviceKt>()
            device_id = device?.id.toString()
        }
        mViewBind.rvList.run {
            vertical()
            adapter = orderKtAdapter
        }
        orderKtAdapter.run {
            setOnItemClickListener { adapter, _, position ->
                val item = adapter.items[position]
                var bundle = Bundle()
                bundle.putString("id", item.id)
                bundle.putSerializable("order", item)
                toStartActivity(ReportInforKtActivity::class.java, bundle)
            }
            isEmptyViewEnable = true
            emptyView = layoutInflater.inflate(R.layout.listitem_empty, mViewBind.rvList, false)
        }
        mViewBind.refreshLayout.run {
            setRefreshHeader(ClassicsHeader(activity))
            setRefreshFooter(ClassicsFooter(activity))
            setOnRefreshListener {
                page = 1
                myOrders()
            }
            setOnLoadMoreListener {
                page++
                myOrders()
            }
        }
    }

    /**
     * 懒加载
     */
    override fun lazyLoadData() {
        showLoading()
        myOrders()
    }

    fun myOrders() {
        mViewModel.getMyReportOrderList(device_id,start_time, end_time, status, keywords, page)
    }

    override fun createObserver() {
        mViewModel.run {
            myorders.observe(viewLifecycleOwner, Observer { resultState ->
                parseState(resultState, {
                    if (page == 1) {
                        datas.clear()
                        if (it?.data != null) {
                            datas.addAll(it.data)
                            if (it.data.size < BaseConfig.page_count) {
                                mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                            } else {
                                mViewBind.refreshLayout.finishRefresh()
                            }
                        } else {
                            mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                        }
                    } else {
                        if (it?.data != null) {
                            datas.addAll(it.data)
                            if (it.data.size < BaseConfig.page_count) {
                                mViewBind.refreshLayout.finishLoadMoreWithNoMoreData()
                            } else {
                                mViewBind.refreshLayout.finishLoadMore()
                            }
                        } else {
                            mViewBind.refreshLayout.finishLoadMoreWithNoMoreData()
                        }
                    }
                    orderKtAdapter.submitList(datas)
                    orderKtAdapter.notifyDataSetChanged()
                }, {
                    Toaster.show(it.errorMsg)
                    mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                })
            })
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            REFRESH_ADDRESS -> {}
            else -> {}
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onBindViewClick() {
//        setOnclickNoRepeat( mViewBind.tvDai) {
//            when (it.id) {
//                R.id.tv_gongdan -> {
//                    toStartActivity(MyOrderActivity::class.java)
//                }
//
//                R.id.tv_dai -> {
//
//                }
//            }
//        }
    }
}